package com.hooker.plugins;

import android.app.Activity;
import android.content.Context;

import com.hooker.base.BaseXposedLoader;
import com.hooker.common.Reflect;
import com.hooker.common.Reflection;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class HttpEntry extends BaseXposedLoader {

    public HttpEntry(Context context, XC_LoadPackage.LoadPackageParam loadParams) {
        super(context, loadParams);
    }

    @Override
    public void hook(Object... calls) {
        log("hook 1");
    }

    @Override
    public void hook(Context context, XC_LoadPackage.LoadPackageParam loadParams) {
        super.hook(context, loadParams);
        log("hook");

        hookAllConstructor("dalvik.system.BaseDexClassLoader", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                log("BaseDexClassLoader " + Arrays.toString(param.args));
                super.afterHookedMethod(param);
                if (param.args[0] instanceof String) {
                    final String arg = (String) param.args[0];
                    if (arg.endsWith("gdt_plugin.jar")) {
                        log("gdt_plugin.jar: 1");
                        final ClassLoader thisObject = (ClassLoader) param.thisObject;
                        final Class<?> aClass = thisObject.loadClass("com.qq.e.comm.plugin.t.a.a");
                        log("gdt_plugin.jar: 2");
                        XposedBridge.hookAllConstructors(aClass, new XC_MethodHook() {
                            @Override
                            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                                try {
                                    log("gdt_plugin.jar: 3");
                                    super.afterHookedMethod(param);
                                    Field j1 = aClass.getDeclaredField("j");
                                    j1.setAccessible(true);
                                    byte[] j = (byte[]) j1.get(param.thisObject);
                                    String s = "";
                                    if(j!=null && j.length > 0){
                                        s = new String(j, "utf-8");
                                    }
                                    String url = (String) Reflection.call(param.thisObject, aClass, "f", new Class[]{});
                                    log("a:" + url + " postData:" + s);
                                } catch (Throwable throwable) {
                                    throwable.printStackTrace();
                                }
                            }
                        });

                        Class<?> aClass1 = thisObject.loadClass("com.qq.e.comm.plugin.util.aj");
                        XposedHelpers.findAndHookMethod(aClass1, "a",String.class,String.class,Object[].class, new XC_MethodHook() {
                            @Override
                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                super.beforeHookedMethod(param);
                                log("aj.a:");
                                for (Object o : param.args) {
                                    log("args:"+o);
                                    
                                    if(o.getClass().isArray())
                                    {
                                        log("args:"+Arrays.toString(((Object[]) o)));
                                    }
                                }
                            }
                        });
                        XposedHelpers.findAndHookMethod("com.qq.e.comm.util.GDTLogger", thisObject,"d", String.class, new XC_MethodHook() {
                            @Override
                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                super.beforeHookedMethod(param);
                                log("GDTLogger.d:"+param.args[0]);
                            }
                        });

                        Class<?> aClass2 = thisObject.loadClass("com.qq.e.comm.plugin.intersitial2.a");
                        XposedBridge.hookAllMethods(aClass2, "onADEvent", new XC_MethodHook() {
                            @Override
                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                super.beforeHookedMethod(param);
                                log("a.onADEvent:"+param.args[0]);

                                Object arg1 = param.args[0];
                                Method mGetType = arg1.getClass().getDeclaredMethod("getType");
                                mGetType.setAccessible(true);
                                Object invoke = mGetType.invoke(arg1);
                                log("a.onADEvent:"+param.args[0]+" getType:"+invoke);

                                Method mGetParas = arg1.getClass().getDeclaredMethod("getParas");
                                mGetParas.setAccessible(true);
                                Object[] paras = ((Object[]) mGetParas.invoke(arg1));
                                log("a.onADEvent:"+param.args[0]+" getParas:"+Arrays.toString(paras));

                                for (Object arg : param.args) {
                                    log("a.onADEvent:"+param.args[0]+" "+param.args+arg);
                                }

                                boolean f = paras.length == 1 && (paras[0] instanceof List);
                                if(f){
                                    String name = ((List) paras[0]).get(0).getClass().getName();
                                    log("a.onADEvent "+param.args[0]+" paras[0]).get(0) :"+name);
                                }


                            }

                            @Override
                            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                                super.afterHookedMethod(param);
                                Field d = param.thisObject.getClass().getDeclaredField("d");
                                d.setAccessible(true);
                                Object o = d.get(param.thisObject);
                                log("a.onADEvent this.d:"+o);
                            }
                        });

                        XposedHelpers.findAndHookMethod("com.qq.e.comm.plugin.gdtnativead.h", thisObject, "a", List.class, new XC_MethodHook() {
                            @Override
                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                super.beforeHookedMethod(param);
                                log("gdtnativead.h.a(List) args:"+param.args[0]);
                                for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
                                    log("gdtnativead.h.a(List) "+stackTraceElement);
                                }
                            }
                        });

                        XposedHelpers.findAndHookMethod("com.qq.e.comm.plugin.intersitial2.e", thisObject, "a", boolean.class, Activity.class, new XC_MethodHook() {
                            @Override
                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                super.beforeHookedMethod(param);
                                log("com.qq.e.comm.plugin.intersitial2.e.a(boolan,Activity)"+Arrays.toString(param.args));
                            }
                        });

                        XposedBridge.hookAllConstructors(thisObject.loadClass("com.qq.e.comm.net.rr.AbstractRequest"),new XC_MethodHook(){
                            @Override
                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                log("AbstractRequest "+ Arrays.toString(param.args));
                                super.beforeHookedMethod(param);
                            }
                        });

                        hookConstructor(URL.class,String.class, new XC_MethodHook() {
                            @Override
                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                log("URL "+Arrays.toString(param.args));
                                super.beforeHookedMethod(param);
                            }
                        });

                        XposedHelpers.findAndHookMethod("com.qq.e.comm.plugin.t.a$b", thisObject,"a", boolean.class, new XC_MethodHook() {
                            @Override
                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                super.beforeHookedMethod(param);
                                Field aFiled = param.thisObject.getClass().getDeclaredField("a");
                                aFiled.setAccessible(true);
                                Object a = aFiled.get(param.thisObject);

                                Field j1 = aClass.getDeclaredField("j");
                                j1.setAccessible(true);
                                byte[] j = (byte[]) j1.get(a);
                                String s = "";
                                if(j!=null && j.length > 0){
                                    s = new String(j, "utf-8");
                                }
                                String url = (String) Reflection.call(a, aClass, "f", new Class[]{});

                                log("com.qq.e.comm.plugin.t.a$b  a:"+a.getClass().getName()+" url:"+ url + " postData:" + s);
                            }
                        });

                        XposedHelpers.findAndHookMethod("com.qq.e.comm.plugin.t.d$b", thisObject,"a", boolean.class, new XC_MethodHook() {
                            @Override
                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                super.beforeHookedMethod(param);
                                Field aFiled = param.thisObject.getClass().getDeclaredField("a");
                                aFiled.setAccessible(true);
                                Object a = aFiled.get(param.thisObject);

                                Field j1 = aClass.getDeclaredField("j");
                                j1.setAccessible(true);
                                byte[] j = (byte[]) j1.get(a);
                                String s = "";
                                if(j!=null && j.length > 0){
                                    s = new String(j, "utf-8");
                                }
                                String url = (String) Reflection.call(a, aClass, "f", new Class[]{});

                                log("com.qq.e.comm.plugin.t.d$b  a:"+a.getClass().getName()+" url:" + url + " postData:" + s);
                            }
                        });

//                        Class<?> clAr = thisObject.loadClass("com.qq.e.comm.plugin.util.ar");
//                        Method a1 = clAr.getDeclaredMethod("a", byte[].class);
//                        a1.setAccessible(true);
                        XposedHelpers.findAndHookMethod("com.qq.e.comm.plugin.util.ar", thisObject, "a", byte[].class, new XC_MethodHook() {
                            @Override
                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                super.beforeHookedMethod(param);
                                byte[] data = (byte[])param.args[0];
                                if(data!=null && data.length >0) {
                                    String s = new String(data);
                                    if(s.contains("com.sdk.ebad")){
                                        for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
                                            log("packNotOk "+stackTraceElement);
                                        }
                                    }
                                    for (int i = 0; i < s.length();) {
                                        log("ar.a:" + s.substring(i,Math.min(i+1024,s.length())));
                                        i += 1024;
                                    }
                                }
                            }
                        });


                    }
                }
            }
        });

        if("com.qq.e.union.demo".equals(loadParams.packageName)){
            try {
                Class<?> aClass2 = loadParams.classLoader.loadClass("com.qq.e.comm.plugin.intersitial2.a");
                XposedBridge.hookAllMethods(aClass2, "onADEvent", new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        super.beforeHookedMethod(param);
                        log("a.onADEvent:"+param.args[0]);

                        Object arg1 = param.args[0];
                        Method mGetType = arg1.getClass().getDeclaredMethod("getType");
                        mGetType.setAccessible(true);
                        Object invoke = mGetType.invoke(arg1);
                        log("a.onADEvent:"+param.args[0]+" getType:"+invoke);

                        Method mGetParas = arg1.getClass().getDeclaredMethod("getParas");
                        mGetParas.setAccessible(true);
                        Object[] paras = ((Object[]) mGetParas.invoke(arg1));
                        log("a.onADEvent:"+param.args[0]+" getParas:"+Arrays.toString(paras));

                        for (Object arg : param.args) {
                            log("a.onADEvent:"+param.args[0]+" "+param.args+arg);
                        }

                        boolean f = paras.length == 1 && (paras[0] instanceof List);
                        if(f){
                            String name = ((List) paras[0]).get(0).getClass().getName();
                            log("a.onADEvent "+param.args[0]+" paras[0]).get(0) :"+name);
                        }


                    }

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        Field d = param.thisObject.getClass().getDeclaredField("d");
                        d.setAccessible(true);
                        Object o = d.get(param.thisObject);
                        log("a.onADEvent this.d:"+o);
                    }
                });
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            hookAllConstructor("com.qq.e.comm.net.rr.AbstractRequest",new XC_MethodHook(){
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    log("AbstractRequest "+ Arrays.toString(param.args));
                    super.beforeHookedMethod(param);
                }
            });

            hookConstructor(URL.class,String.class, new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    log("URL "+Arrays.toString(param.args));
                    super.beforeHookedMethod(param);
                }
            });
        }



    }
}
